package cn.jx.asc.common.interceptor;

import cn.jx.asc.user.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 周国伟
 * @desc TODO
 * @date 2018/6/4 14:16
 * @describe 前端后台页面拦截器
 */
public class SessionInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String url = request.getRequestURI(); //访问路径中含有登入路径的就放过
        if(url.indexOf("Login")>=0){
            return true;
        }
        HttpSession session = request.getSession();  //用户已经登入过的放过
        User user = (User) session.getAttribute("USER_SESSION");
        if (user!=null){
            return true;
        }
        //不符合以上条件的就转发拦截跳转到登入页面
        //request.getRequestDispatcher("/Login/login").forward(request,response);
        //不符合以上条件的就重定向拦截跳转到登入页面
        response.sendRedirect("/Login/login");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
